package com.hup.joystick.view.quickMenu.setting;

import com.hup.utils.swing.customView.BaseFileChooser.ChoseListener;
import com.hup.utils.swing.customView.FileChooserPanel;
import com.hup.utils.swing.customView.ImageButton;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * 程序item
 *
 * @author hugan
 * @date 2020/11/11
 */
public class QMSAppItem extends JPanel {
    public JPanel root;
    public ImageButton btnUp;
    public ImageButton btnDown;
    public FileChooserPanel txtApp;

    private AppEditListener editListener;

    public QMSAppItem() {
        $$$setupUI$$$();
        txtApp.setExts(new String[]{"exe", "jar"});

        ActionListener al = e -> {
            if (editListener != null)
                editListener.onMove(QMSAppItem.this, e.getSource() == btnUp);
        };
        btnUp.setActionListener(al);
        btnDown.setActionListener(al);
    }

    private void createUIComponents() {
        root = this;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        root.setLayout(new GridBagLayout());
        btnUp = new ImageButton();
        btnUp.setIcon(new ImageIcon(getClass().getResource("/img/arrow_up.gif")));
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        root.add(btnUp, gbc);
        btnDown = new ImageButton();
        btnDown.setIcon(new ImageIcon(getClass().getResource("/img/arrow_down.gif")));
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        root.add(btnDown, gbc);
        txtApp = new FileChooserPanel();
        txtApp.setChooserBtnTxt("选择");
        txtApp.setHint("双击添加程序");
        txtApp.setLabel("");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(txtApp, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() { return root; }

    public void setEditListener(AppEditListener editListener) {
        this.editListener = editListener;
        txtApp.setChoseListener(editListener);
    }

    public void setApp(String appPath) {
        txtApp.setSelectedFilePath(appPath);
    }

    public String getSelectedFilePath() {
        return txtApp.getSelectedFilePath();
    }

    public interface AppEditListener extends ChoseListener {

        @Override
        void afterChose(String chosePath);

        void onMove(QMSAppItem item, boolean isUp);
    }

}
